/*
 <EasySinhala. An English to Sinhala translitratior.>
 Copyright (C) 2009  S. Suhothayan

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 this is a product by S.Suhothayan
 CSE
 University of Moratuwa
 */

// The only global object for this extension.
var easysinhala = {};

// Basic namespace implementation.
(function() {

    var namespaces = [];

    // Namespace registration
    this.ns = function(fn) {
        var ns = {};
        namespaces.push(fn, ns);
        return ns;
    };

    // Namespace initialization
    this.initialize = function() {
        for (var i = 0; i < namespaces.length; i += 2) {
            var fn = namespaces[i];
            var ns = namespaces[i + 1];
            fn.apply(ns);
        }
    };


    this.initializeButton = function() {
        const easysinhalaItemID = "easysinhalaButton";
        const easysinhalaItemInstalled = "easysinhala.toolbariteminstalled";
        var toolbox = document.getElementById("navigator-toolbox");
        var hasFoxTabButton = false;
        if (toolbox.hasAttribute(easysinhalaItemInstalled)) {
            return false;
        }
        for (var i = 0; i < toolbox.childNodes.length; ++i) {
            var toolbar = toolbox.childNodes[i];
            if (toolbar.localName == "toolbar" && toolbar.getAttribute("customizable") == "true") {
                if (toolbar.currentSet.indexOf("easysinhalaButton") > -1)
                    hasFoxTabButton = true;
            }
        }
        if (!hasFoxTabButton) {
            const easysinhalaToolBarID = "nav-bar";
            var tbElem = document.getElementById(easysinhalaToolBarID);
            if (tbElem) {
                var newSet = tbElem.currentSet + "," + easysinhalaItemID;
                tbElem.setAttribute("currentset", newSet);
                tbElem.currentSet = newSet;
                document.persist(easysinhalaToolBarID, "currentset");
                try {
                    toolbox.setAttribute(easysinhalaItemInstalled, "initialized");
                    toolbox.ownerDocument.persist(toolbox.id, easysinhalaItemInstalled);
                    BrowserToolboxCustomizeDone(true);
                } catch (e) {
                }
            }
        }
    };

    // Clean up
    this.shutdown = function() {
        window.removeEventListener("load", easysinhala.initialize, false);
        window.removeEventListener("unload", easysinhala.shutdown, false);
    };

    // Adding toolbar button
    window.addEventListener("load", easysinhala.initializeButton, false);

    // Register handlers to maintain extension life cycle.
    window.addEventListener("load", easysinhala.initialize, false);
    window.addEventListener("unload", easysinhala.shutdown, false);

}).apply(easysinhala);
